package com.lechi.web.runner;

import com.lechi.web.constants.Constants;
import com.lechi.web.job.GetRecordFileNameJob;
import com.lechi.web.quartz.scheduler.DynamicJob;
import com.lechi.web.quartz.scheduler.DynamicSchedulerFactory;
import com.lechi.web.service.IIotGatewayService;
import com.lechi.web.system.domain.IotDeviceBo;
import com.lechi.web.system.domain.IotGatewayBo;
import com.lechi.web.system.mapper.iot.IotDeviceMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.lechi.web.framework.util.GuavaCacheUtil;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 开启网关端口监听，更新网关状态打开，初始化设备状态为未连接
 *
 * @author yangguang
 */
@Slf4j
@Order(2)
@Component("StartTCPGateWay")
public class StartTCPGateWay implements CommandLineRunner {

    @Autowired
    private IIotGatewayService iotGatewayService;

    @Autowired
    private IotDeviceMapper iotDeviceMapper;

    @Autowired
    private DynamicSchedulerFactory dynamicSchedulerFactory;

    public void run(String... args) throws SchedulerException {
        iotGatewayService.selectIotGatewayList(new IotGatewayBo()).forEach(iotDevice -> {
            iotGatewayService.start(iotDevice);
            IotGatewayBo gateway = new IotGatewayBo();
            if (Objects.equals(iotDevice.getStatus(), Constants.CLOSE_STATUE)) {
                gateway.setId(iotDevice.getId());
                gateway.setStatus(Constants.SOCKT_OPEN_STATUE);
                iotGatewayService.updateIotGateway(gateway);
            }
        });
        List<IotDeviceBo> iotDeviceBoList = iotDeviceMapper.selectIotDeviceListForSchedule();
        iotDeviceBoList.forEach(iotDeviceBo -> {
            GuavaCacheUtil.getBiDeviceSnAndIPMap().forcePut(iotDeviceBo.getDeviceSn(), iotDeviceBo.getDeviceSn());
            initWriteTask(iotDeviceBo);
        });


    }

    /**
     * 初始化写日志文件任务
     */
    public void initWriteTask(IotDeviceBo iotDeviceBo) {
        if (Objects.isNull(iotDeviceBo.getIsRecordLog()) || iotDeviceBo.getIsRecordLog() != 1 || iotDeviceBo.getRecordLogTime() == null) {
            return;
        }
        String jobName = Constants.WRITE_RECORD_LOG_NAME + iotDeviceBo.getId();
        try {
            DynamicJob writeRecordLogJob = new DynamicJob(jobName)
                    .cronExpression(String.valueOf(iotDeviceBo.getRecordLogTime() * 60))
                    .target(GetRecordFileNameJob.class);
            writeRecordLogJob.addJobData("deviceId", iotDeviceBo.getId());
            dynamicSchedulerFactory.removeJob(writeRecordLogJob);
            dynamicSchedulerFactory.registerJob(writeRecordLogJob, new Date());
            log.info("注册调度CreateRecordLogJob任务 [{}] -> ok", writeRecordLogJob);
        } catch (SchedulerException e) {
            log.error("注册调度CreateRecordLogJob任务 异常", e);
        }
    }
}


